package com.junzhiit.nengji.cms.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.cms.service.SystemUserService;
import com.junzhiit.nengji.cms.service.WhiteListPhoneService;
import com.junzhiit.nengji.cms.web.util.aliyun.SmsUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserAddVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserEditVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserListVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/system/user")
public class SystemUserController extends BaseController {

	@Autowired
	private SystemUserService systemUserService;

	@Autowired
	private WhiteListPhoneService whiteListPhoneService;

	@ApiOperation(value = "获取所有用户列表")
	@RequestMapping(method = RequestMethod.GET)
	public Map<String, Object> findAll(DataTablesParamVO dtp, SystemUserListVO systemUserList) {
		return systemUserService.findAll(dtp, systemUserList);
	}

	@ApiOperation(value = "创建系统用户")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO create(SystemUserAddVO systemUserAddVO) {
		AssertUtil.notBlank(systemUserAddVO.getMobilePhone(), "mobile phone is required.");
		AssertUtil.notBlank(systemUserAddVO.getPassword(), "password is required.");
		AssertUtil.notBlank(systemUserAddVO.getConfirmPassword(), "confirm password is required.");
		AssertUtil.notNull(systemUserAddVO.getIsUpdatePassword(), "IsUpdatePassword is required.");
		AssertUtil.notBlank(systemUserAddVO.getName(), "name is required.");
		AssertUtil.notNull(systemUserAddVO.getGender(), "gender is required.");
		AssertUtil.notBlank(systemUserAddVO.getEmail(), "email is required.");
		return systemUserService.add(systemUserAddVO);
	}

	@ApiOperation(value = "根据ID查找用户")
	@RequestMapping(value = "/{systemUserId}", method = RequestMethod.GET)
	public ResultVO findById(@PathVariable String systemUserId) {
		AssertUtil.notBlank(systemUserId, "systemUserId is required.");
		ResultVO result = new ResultVO();
		result.setData(systemUserService.findById(systemUserId));
		return result;
	}
	
	@ApiOperation(value = "根据sessionId查找用户")
	@RequestMapping(value = "/find", method = RequestMethod.GET)
	public ResultVO findBySessionId(String sessionId) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		ResultVO result = new ResultVO();
		result.setData(systemUserService.findBySessionId(sessionId));
		return result;
	}
	
	@ApiOperation(value="获取手机验证码(修改当前系统用户)")
	@RequestMapping(value = "/getMobilePhoneCode", method = RequestMethod.GET)
	public ResultVO getMobilePhoneCode(String mobilePhone){
		AssertUtil.notBlank(mobilePhone, "mobile phone is required.");
		String smsModel=SmsUtil.ALIYUN_SMS_VALIDATION;
		return SmsUtil.sendPhoneCode(mobilePhone, smsModel);
	}
	
	@ApiOperation(value = "修改系统当前用户")
	@RequestMapping(value = "/now/{id}", method = RequestMethod.PUT)
	public ResultVO updateUser(@PathVariable String id, SystemUserEditVO systemUserEditVO,HttpServletResponse response) {
		AssertUtil.notBlank(id, "id is required.");
		AssertUtil.notBlank(systemUserEditVO.getMobilePhone(), "mobile phone is required.");
		AssertUtil.notBlank(systemUserEditVO.getName(), "name is required.");
		AssertUtil.notNull(systemUserEditVO.getGender(), "gender is required.");
		AssertUtil.notBlank(systemUserEditVO.getEmail(), "email is required.");
		return systemUserService.editUser(id, systemUserEditVO,response);
	}

	@ApiOperation(value = "修改系统用户")
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public ResultVO update(@PathVariable String id, SystemUserEditVO systemUserEditVO) {
		AssertUtil.notBlank(id, "id is required.");
		AssertUtil.notBlank(systemUserEditVO.getMobilePhone(), "mobile phone is required.");
		AssertUtil.notNull(systemUserEditVO.getIsUpdatePassword(), "IsUpdatePassword is required.");
		AssertUtil.notBlank(systemUserEditVO.getName(), "name is required.");
		AssertUtil.notNull(systemUserEditVO.getGender(), "gender is required.");
		AssertUtil.notBlank(systemUserEditVO.getEmail(), "email is required.");
		return systemUserService.edit(id, systemUserEditVO);
	}

	@ApiOperation(value = "根据SystemId 冻结/解冻商户账号")
	@RequestMapping(value = "/update/status/{id}", method = RequestMethod.PUT)
	public ResultVO updateStatus(@PathVariable String id, Integer status) {
		AssertUtil.notBlank(id, "id is required.");
		return systemUserService.updateStatus(id, status);
	}

	@ApiOperation(value = "检查手机号码是否被注册")
	@RequestMapping(value = "/check/mobilePhone", method = RequestMethod.GET)
	public ResultVO checkPhone(String mobilePhone) {
		AssertUtil.notBlank(mobilePhone, "mobilePhone is required.");
		ResultVO result = new ResultVO();
		result.setData(systemUserService.checkPhone(mobilePhone));
		return result;
	}

	@ApiOperation(value = "获取手机白名单")
	@RequestMapping(value = "/white/list/phone", method = RequestMethod.GET)
	public Map<String, Object> findAllWhiteListPhone(String sessionId) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return whiteListPhoneService.findAll(sessionId);
	}

	@ApiOperation(value = "检验手机白名单是否存在该手机")
	@RequestMapping(value = "/check/white/list/phone", method = RequestMethod.GET)
	public ResultVO checkWhiteListPhone(String phone, String sessionId) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return whiteListPhoneService.checkWhiteListPhone(sessionId, phone);
	}

	@ApiOperation(value = "创建手机白名单")
	@RequestMapping(value = "/white/list/phone", method = RequestMethod.POST)
	public ResultVO addWhiteListPhone(String phone, String sessionId) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return whiteListPhoneService.addWhiteListPhone(sessionId, phone);
	}

	@ApiOperation(value = "删除手机白名单")
	@RequestMapping(value = "/white/list/phone", method = RequestMethod.DELETE)
	public ResultVO delWhiteListPhone(String sessionId, String phone) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return whiteListPhoneService.delWhiteListPhone(sessionId, phone);
	}

	@ApiOperation(value = "获取用户功能导航栏")
	@RequestMapping(value = "/functions", method = RequestMethod.GET)
	public ResultVO getSystemUserFunctions(String sessionId) {
		AssertUtil.notBlank(sessionId, "sessionId is required.");
		return systemUserService.getSystemUserFunctions(sessionId);
	}

	@ApiOperation(value = "更新用户状态")
	@RequestMapping(value = "/status/{id}", method = RequestMethod.PUT)
	public ResultVO updateSystemUserStatus(@PathVariable String id, int status) {
		AssertUtil.notBlank(id, "sessionId is required.");
		return systemUserService.updateSystemUserStatus(id, status);
	}

	@ApiOperation(value = "初始化权限")
	@RequestMapping(value = "/init/power", method = RequestMethod.PUT)
	public ResultVO initPower(SystemUserAddVO systemUserAddVO) {
		return systemUserService.initPower(systemUserAddVO);
	}
	
	@ApiOperation(value = "取消/激活代理商帐号")
	@RequestMapping(value = "/switchActivationStatus", method = RequestMethod.PUT)
	public ResultVO switchActivationStatus(
			@RequestParam("systemId") String systemId,
			@RequestParam("status") int status) {
		return systemUserService.switchActivationStatus(systemId, status);
	}
}
